package com.yf.zzmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.exception.ServiceException;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectDetailsDTO;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectPageDTO;
import com.yf.zzmusic.pojo.dto.musicCollect.MusicCollectUpdateDTO;
import com.yf.zzmusic.pojo.entity.MusicCollect;
import com.yf.zzmusic.mapper.MusicCollectMapper;
import com.yf.zzmusic.pojo.vo.music.MusicTableVO;
import com.yf.zzmusic.pojo.vo.musicCollect.MusicCollectDetailsVO;
import com.yf.zzmusic.service.IMusicCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 歌单表 服务实现类
 * </p>
 *
 * @author
 * @since 2024-02-26
 */
@Service
public class MusicCollectServiceImpl extends ServiceImpl<MusicCollectMapper, MusicCollect> implements IMusicCollectService {

  @Autowired
  MusicCollectMapper musicCollectMapper;

  @Override
  public boolean updateMusicCollect(MusicCollectUpdateDTO musicCollectUpdateDTO) {
    if (getById(musicCollectUpdateDTO.getMusicCollectId()) == null) {
      throw new ServiceException(Constants.NO_CONTENT, "歌单未找到");
    }
    MusicCollect musicCollect = new MusicCollect();
    //拷贝
    BeanUtils.copyProperties(musicCollectUpdateDTO, musicCollect);
    return updateById(musicCollect);
  }

  @Override
  public MusicCollectDetailsVO musicCollectDetailsByMusicCollectId(MusicCollectDetailsDTO musicCollectDetailsDTO) {
    //先取值
    Integer musicCollectId = musicCollectDetailsDTO.getMusicCollectId();
    Integer pageNum = musicCollectDetailsDTO.getPageNum();
    Integer pageSize = musicCollectDetailsDTO.getPageSize();
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum=1;
      musicCollectDetailsDTO.setPageNum(1);
    }
    if (pageSize == null) {
      pageSize = 10;
      musicCollectDetailsDTO.setPageSize(10);
    }
    //创建返回对象
    MusicCollectDetailsVO musicCollectDetailsVO = new MusicCollectDetailsVO();


    //执行自定义查询,获取音乐列表
    if (pageNum >= 1) {
      musicCollectDetailsDTO.setPageNum((pageNum - 1) * pageSize);
    }

    musicCollectDetailsDTO.setPageSize(pageSize);
    musicCollectDetailsDTO.setPageNum(pageNum);

    Integer totalCount = musicCollectMapper.countDetailsById(musicCollectDetailsDTO);

    List<MusicTableVO> musicTableVOList = musicCollectMapper.musicCollectDetailsById(musicCollectDetailsDTO);


    //获取音乐详情
    MusicCollect musicCollect = musicCollectMapper.selectById(musicCollectId);
    if (musicCollect == null) {
      throw new ServiceException(Constants.NO_CONTENT, "歌单未找到");
    }
    //对象复制
    BeanUtils.copyProperties(musicCollect, musicCollectDetailsVO);
    //赋值音乐列表
    musicCollectDetailsVO.setMusicList(musicTableVOList);
    //赋值总条数
    musicCollectDetailsVO.setTotal(totalCount);
    musicCollectDetailsVO.setCurrent(musicCollectDetailsDTO.getPageNum());
    musicCollectDetailsVO.setSize(musicCollectDetailsDTO.getPageSize());
    return musicCollectDetailsVO;
  }

  @Override
  public PageInfo musicByPage(MusicCollectPageDTO musicCollectPageDTO) {
    QueryWrapper<MusicCollect> queryWrapper = new QueryWrapper<>();
    Integer pageNum = musicCollectPageDTO.getPageNum();
    Integer pageSize = musicCollectPageDTO.getPageSize();
    String name = musicCollectPageDTO.getName();
    String createUserNickname = musicCollectPageDTO.getCreateUserNickname();
    Integer createUserId = musicCollectPageDTO.getCreateUserId();
    Integer type = musicCollectPageDTO.getType();
    //判断pageNum与pageSize
    if (pageNum == null) {
      pageNum = 0;
    }
    if (pageSize == null) {
      pageSize = 1000;
    }
    if (!StringUtils.isEmpty(name)) {
      queryWrapper.like("name", name);
    }
    if (!StringUtils.isEmpty(createUserNickname)) {
      queryWrapper.like("create_user_nickname", createUserNickname);
    }
    if (createUserId != null) {
      queryWrapper.eq("create_user_id", createUserId);
    }
    if (type != null) {
      queryWrapper.eq("type", type);
    }
    Page<MusicCollect> musicCollectPage = page(new Page<>(pageNum, pageSize), queryWrapper);
    PageInfo<MusicCollect> musicCollectPageInfo = new PageInfo<>();
    musicCollectPageInfo.setPageNum(pageNum);
    musicCollectPageInfo.setPageSize(pageSize);
    musicCollectPageInfo.setTotal(musicCollectPage.getTotal());
    musicCollectPageInfo.setData(musicCollectPage.getRecords());
    return musicCollectPageInfo;
  }
}
